import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 125
    Component.onCompleted: {
        generalParaLoader.visible=true
        gsTimmer.start();
        mymodbus.getStabilityRange()
        mymodbus.getClearRange()
        mymodbus.getPowerAutoZero()
        mymodbus.getZeroTraceRange()
        mymodbus.getADSampleRate()
        mymodbus.getMaxRange()
    }
    Timer{
        id:gsTimmer
        interval: 1200
        running: false
        repeat: false
        onTriggered: generalParaLoader.visible = false
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: generalParaLoader
        visible:true
        z:101
    }
    Dialog {
        anchors.centerIn: parent
        id: saveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        title:qsTr("保存成功！")
        modal: true
        closePolicy: "CloseOnEscape"
        //onAccepted: saveOKDialog.close()
        z:100
        //onRejected: console.log("Cancel clicked")
    }

    function getClearRange()
    {
        var range =[];
        for(var i=0; i<100; i++)
        {
            range.push(i);
        }
        return range;
    }
    Rectangle{
        id: gsMainRec
        anchors.fill: parent
        color:"transparent"
        Rectangle
        {
            color:"transparent"
            width: 800
            height: 280
            GridLayout{
                id: generalSettingsLayout
                rows: 3
                columns: 6
                anchors.centerIn: parent
                rowSpacing: 40
                Button
                {
                    Layout.row:0
                    Layout.column:0
                    Layout.preferredWidth: btnWidth
                    Layout.preferredHeight: btnHeight
                    // x: 152
                    // y: 13
                    text: qsTr("判稳范围")
                    font.pixelSize: 18
                    background: Rectangle{
                        color: btnColor
                            }
                    onClicked: {
                        cmbStabilityRange.popup.open()
                        //miniKeyBoard.lineValue = "";
                        //miniKeyBoard.textInput=oldPwd;
                    }
                }
                MyComboBox
                {
                    id:cmbStabilityRange
                    Layout.row:0
                    Layout.column:1
                    Layout.preferredWidth: btnWidth
                    Layout.preferredHeight: btnHeight
                    borderColor:"white"
                    fillColor:"#0A3653"
                    model:[1,2,3,4,5,6,7,8,9]
                    currentIndex: cmbStabilityRange.find(mymodbus.stabilityRange)
                }
    //            Rectangle {
    //                id: recStabilityRange
    //                width:300
    //                height:30
    //                // x: 218
    //                // y: 7
    //                color: "transparent"
    //                border.width: 0
    //                border.color: "white"
    //                Layout.row:0
    //                Layout.column:1
    //                Layout.preferredWidth: btnWidth
    //                Layout.preferredHeight: btnHeight
    //                // 绘制一条边框
    //                Shape {
    //                    anchors.fill: parent
    //                    ShapePath {
    //                        strokeWidth: 3
    //                        strokeColor: "white"
    //                        strokeStyle: ShapePath.SolidLine
    //                        startX: 0
    //                        startY: recStabilityRange.height
    //                        PathLine {
    //                            x: recStabilityRange.width
    //                            y: recStabilityRange.height
    //                        }
    //                    }
    //                }
    //                TextInput{
    //                    id:stabilityRange
    //                    anchors.fill: parent
    //                    echoMode: TextInput.Password
    //                    color:"white"
    //                    font.pixelSize: 25
    //                    horizontalAlignment: Text.AlignHCenter
    //                    MouseArea {
    //                           anchors.fill: parent
    //                           onClicked: {
    //                               //miniKeyBoard.lineValue = "";
    //                               //miniKeyBoard.textInput=oldPwd;
    //                           }
    //                       }

    //                }
    //            }
                    Button
                    {
                        Layout.row:0
                        Layout.column:2
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // x: 152
                        // y: 13
                        text: qsTr("清零范围")
                        font.pixelSize: 18
                        background: Rectangle{
                            color: btnColor
                                }
                        onClicked: {
                            cmbClearRange.popup.open()
                            //miniKeyBoard.lineValue = "";
                            //miniKeyBoard.textInput=oldPwd;
                        }
                    }
                    MyComboBox
                    {
                        id:cmbClearRange
                        Layout.row:0
                        Layout.column:3
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        borderColor:"white"
                        fillColor:"#0A3653"
                        model:getClearRange()
                        currentIndex: cmbClearRange.find(mymodbus.clearRange)
                    }
    //                Rectangle {
    //                    id: recClearRange
    //                    width:300
    //                    height:30
    //                    // x: 218
    //                    // y: 7
    //                    color: "transparent"
    //                    border.width: 0
    //                    border.color: "white"
    //                    Layout.row:0
    //                    Layout.column:3
    //                    Layout.preferredWidth: btnWidth
    //                    Layout.preferredHeight: btnHeight
    //                    // 绘制一条边框
    //                    Shape {
    //                        anchors.fill: parent
    //                        ShapePath {
    //                            strokeWidth: 3
    //                            strokeColor: "white"
    //                            strokeStyle: ShapePath.SolidLine
    //                            startX: 0
    //                            startY: recClearRange.height
    //                            PathLine {
    //                                x: recClearRange.width
    //                                y: recClearRange.height
    //                            }
    //                        }
    //                    }
    //                    TextInput{
    //                        id:clearRange
    //                        anchors.fill: parent
    //                        echoMode: TextInput.Password
    //                        color:"white"
    //                        font.pixelSize: 25
    //                        horizontalAlignment: Text.AlignHCenter
    //                        MouseArea {
    //                               anchors.fill: parent
    //                               onClicked: {
    //                                   // miniKeyBoard.lineValue = "";
    //                                   // miniKeyBoard.textInput=oldPwd;
    //                               }
    //                           }

    //                    }
    //                }
                    Button
                    {
                        Layout.row:0
                        Layout.column:4
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // x: 152
                        // y: 13
                        text: qsTr("上电自动清零")
                        font.pixelSize: 18
                        background: Rectangle{
                            color: btnColor
                                }
                        onClicked: {
                            cmbPowerAutoZero.popup.open()
                            //miniKeyBoard.lineValue = "";
                            //miniKeyBoard.textInput=oldPwd;
                        }
                    }

                    MyComboBox
                    {
                        id:cmbPowerAutoZero
                        Layout.row:0
                        Layout.column:5
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        borderColor:"white"
                        fillColor:"#0A3653"
                        model:["OFF", "ON"]
                        currentIndex:{
                            if(mymodbus.powerAutoZero === 1){
                                return 1
                            }
                            else{
                                return 0
                            }
                        }
                    }
    //                Rectangle {
    //                    id: recPowerAutoZero
    //                    width:300
    //                    height:30
    //                    // x: 218
    //                    // y: 7
    //                    color: "transparent"
    //                    border.width: 0
    //                    border.color: "white"
    //                    Layout.row:0
    //                    Layout.column:5
    //                    Layout.preferredWidth: btnWidth
    //                    Layout.preferredHeight: btnHeight
    //                    // 绘制一条边框
    //                    Shape {
    //                        anchors.fill: parent
    //                        ShapePath {
    //                            strokeWidth: 3
    //                            strokeColor: "white"
    //                            strokeStyle: ShapePath.SolidLine
    //                            startX: 0
    //                            startY: recPowerAutoZero.height
    //                            PathLine {
    //                                x: recPowerAutoZero.width
    //                                y: recPowerAutoZero.height
    //                            }
    //                        }
    //                    }
    //                    TextInput{
    //                        id:powerAutoZero
    //                        anchors.fill: parent
    //                        echoMode: TextInput.Password
    //                        color:"white"
    //                        font.pixelSize: 25
    //                        horizontalAlignment: Text.AlignHCenter
    //                        MouseArea {
    //                               anchors.fill: parent
    //                               onClicked: {
    //                                   // miniKeyBoard.lineValue = "";
    //                                   // miniKeyBoard.textInput=oldPwd;
    //                               }
    //                           }

    //                    }
    //                }
                    Button
                    {
                        Layout.row:1
                        Layout.column:0
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // x: 152
                        // y: 13
                        text: qsTr("零点跟踪范围")
                        font.pixelSize: 18
                        background: Rectangle{
                            color: btnColor
                                }
                        onClicked: {
                            cmbZeroTraceRange.popup.open()
                            //miniKeyBoard.lineValue = "";
                            //miniKeyBoard.textInput=oldPwd;
                        }
                    }

                    MyComboBox
                    {
                        id:cmbZeroTraceRange
                        Layout.row:1
                        Layout.column:1
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        borderColor:"white"
                        fillColor:"#0A3653"
                        model:[0,1,2,3,4,5,6,7,8,9]
                        currentIndex: cmbZeroTraceRange.find(mymodbus.zeroTraceRange)
                    }
    //                Rectangle {
    //                    id: recZeroTraceRange
    //                    width:300
    //                    height:30
    //                    // x: 218
    //                    // y: 7
    //                    color: "transparent"
    //                    border.width: 0
    //                    border.color: "white"
    //                    Layout.row:1
    //                    Layout.column:1
    //                    Layout.preferredWidth: btnWidth
    //                    Layout.preferredHeight: btnHeight
    //                    // 绘制一条边框
    //                    Shape {
    //                        anchors.fill: parent
    //                        ShapePath {
    //                            strokeWidth: 3
    //                            strokeColor: "white"
    //                            strokeStyle: ShapePath.SolidLine
    //                            startX: 0
    //                            startY: recZeroTraceRange.height
    //                            PathLine {
    //                                x: recZeroTraceRange.width
    //                                y: recZeroTraceRange.height
    //                            }
    //                        }
    //                    }
    //                    TextInput{
    //                        id:zeroTraceRange
    //                        anchors.fill: parent
    //                        echoMode: TextInput.Password
    //                        color:"white"
    //                        font.pixelSize: 25
    //                        horizontalAlignment: Text.AlignHCenter
    //                        MouseArea {
    //                               anchors.fill: parent
    //                               onClicked: {
    //                                   //miniKeyBoard.lineValue = "";
    //                                   //miniKeyBoard.textInput=oldPwd;
    //                               }
    //                           }

    //                    }
    //                }
                    Button
                    {
                        Layout.row:1
                        Layout.column:2
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // x: 152
                        // y: 13
                        text: qsTr("AD采样速率")
                        font.pixelSize: 18
                        background: Rectangle{
                            color: btnColor
                                }
                        onClicked: {
                            cmbADSampleRate.popup.open()
                            //miniKeyBoard.lineValue = "";
                            //miniKeyBoard.textInput=oldPwd;
                        }
                    }

                    MyComboBox
                    {
                        id:cmbADSampleRate
                        Layout.row:1
                        Layout.column:3
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        borderColor:"white"
                        fillColor:"#0A3653"
                        model:[qsTr("120次/s"),qsTr("480次/s"),qsTr("960次/s")]
                        currentIndex: mymodbus.aDSampleRate
                    }
    //                Rectangle {
    //                    id: recADSampleRate
    //                    width:300
    //                    height:30
    //                    // x: 218
    //                    // y: 7
    //                    color: "transparent"
    //                    border.width: 0
    //                    border.color: "white"
    //                    Layout.row:1
    //                    Layout.column:3
    //                    Layout.preferredWidth: btnWidth
    //                    Layout.preferredHeight: btnHeight
    //                    // 绘制一条边框
    //                    Shape {
    //                        anchors.fill: parent
    //                        ShapePath {
    //                            strokeWidth: 3
    //                            strokeColor: "white"
    //                            strokeStyle: ShapePath.SolidLine
    //                            startX: 0
    //                            startY: recADSampleRate.height
    //                            PathLine {
    //                                x: recADSampleRate.width
    //                                y: recADSampleRate.height
    //                            }
    //                        }
    //                    }
    //                    TextInput{
    //                        id:adSampleRate
    //                        anchors.fill: parent
    //                        echoMode: TextInput.Password
    //                        color:"white"
    //                        font.pixelSize: 25
    //                        horizontalAlignment: Text.AlignHCenter
    //                        MouseArea {
    //                               anchors.fill: parent
    //                               onClicked: {
    //                                   // miniKeyBoard.lineValue = "";
    //                                   // miniKeyBoard.textInput=oldPwd;
    //                               }
    //                           }

    //                    }
    //                }
                    Button
                    {
                        Layout.row:1
                        Layout.column:4
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // x: 152
                        // y: 13
                        text: qsTr("最大量程")
                        font.pixelSize: 18
                        background: Rectangle{
                            color: btnColor
                                }
                        onClicked: {
                            //miniKeyBoard.lineValue = "";
                            //miniKeyBoard.textInput=oldPwd;
                           //rectangle.selectedView=16
                           enterNumberPanel.lastButtonIndex = 4;
                           enterNumberPanel.lastViewIndex =8;
                           enterNumberPanel.minimum=0;
                           enterNumberPanel.clearText()
                           enterNumberPanel.maximum=100000*mymodbus.minGraduation>999999?999999:100000*mymodbus.minGraduation
                           enterNumberPanel.itemName=qsTr("最大量程")
                           enterNumberPanel.itemValue=maxRange.text
                           enterNumberPanel.textInput=maxRange;

                            enterNumberPanel.visible=true;
                            enterNumberPanel.lastRec=gsMainRec
                            gsMainRec.visible=false
                        }
                    }
                    Rectangle {
                        id: recMaxRange
                        color: "transparent"
                        border.width: 0
                        border.color: "white"
                        Layout.row:1
                        Layout.column:5
                        Layout.preferredWidth: btnWidth
                        Layout.preferredHeight: btnHeight
                        // 绘制一条边框
                        Shape {
                            anchors.fill: parent
                            ShapePath {
                                strokeWidth: 3
                                strokeColor: "white"
                                strokeStyle: ShapePath.SolidLine
                                startX: 0
                                startY: recMaxRange.height
                                PathLine {
                                    x: recMaxRange.width
                                    y: recMaxRange.height
                                }
                            }
                        }
                        TextInput{
                            id:maxRange
                            anchors.fill: parent
                            //echoMode: TextInput.Password
                            text:mymodbus.maxRange
                            color:"white"
                            font.pixelSize: 25
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignBottom
                            MouseArea {
                                   anchors.fill: parent
                                   onClicked: {
                                       // miniKeyBoard.lineValue = "";
                                       // miniKeyBoard.textInput=oldPwd;
                                       //rectangle.selectedView=16
                                       enterNumberPanel.lastButtonIndex = 4;
                                       enterNumberPanel.lastViewIndex =8;
                                       enterNumberPanel.minimum=0;
                                       enterNumberPanel.clearText()
                                       enterNumberPanel.maximum=100000*mymodbus.minGraduation>999999?999999:100000*mymodbus.minGraduation
                                       enterNumberPanel.itemName=qsTr("最大量程")
                                       enterNumberPanel.itemValue=maxRange.text
                                       enterNumberPanel.textInput=maxRange;
                                       enterNumberPanel.visible=true;
                                       enterNumberPanel.lastRec=gsMainRec
                                       gsMainRec.visible=false
                                   }
                               }

                        }
                    }

            }

        }
        Button
        {
            id:btnAdvSettingsOK
            x: 572
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#C070B0"
                    }

            text: qsTr("确定")
            font.pixelSize: 25
            palette.buttonText: "white"
            Connections {
                target: btnAdvSettingsOK
                function onClicked() {
                    //mymodbus.getMinGraduation()
    //                if(Number(maxRange.text)>100000*mymodbus.minGraduation)
    //                {
    //                    maxRangeIllegalDialog.open()
    //                    return;
    //                }
                    generalParaLoader.visible=true
                    var result =
                    mymodbus.setStabilityRange(cmbStabilityRange.currentValue)
                    && mymodbus.setClearRange(cmbClearRange.currentValue)
                    && mymodbus.setPowerAutoZero(cmbPowerAutoZero.currentIndex)
                    && mymodbus.setZeroTraceRange(cmbZeroTraceRange.currentValue)
                    && mymodbus.setADSampleRate(cmbADSampleRate.currentIndex)
                    && mymodbus.setMaxRange(Number(maxRange.text))
                    generalParaLoader.visible=false
                    if(result)
                    {
                        saveOKDialog.title=qsTr("保存成功！")
                        saveOKDialog.open()
                        saveOKDialog.accepted.connect(function(){
                            saveOKDialog.close()
                            //rectangle.selectedView=0
                            bottomBar.selected=0
//                            myLoader.visible=false;
//                            myLoader.visible=true;
                            myLoader.source="StatusView.qml"
                            //statusView.visible=true;
                        })
                    }
                    else{
                        saveOKDialog.title=qsTr("保存失败！")
                        saveOKDialog.open()
                        saveOKDialog.accepted.connect(function(){
                            saveOKDialog.close()
                        })
                    }

                }
            }
        }
       Button
        {
            id:btnReturn
            x: 688
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#B0B0D0"
                    }

            text: qsTr("返回")
            font.pixelSize: 25
            palette.buttonText: "white"
            Connections {
                target: btnReturn
                function onClicked() {
                    //rectangle.selectedView = 4
                    bottomBar.selected=4
//                    myLoader.visible=false;
//                    myLoader.visible=true
                    myLoader.source="SettingsPanel.qml"
                }
            }
        }

    }
    EnterNumberPanel{
          id:enterNumberPanel
          width: 800
          height: 391
          visible:false
    }

}

